/**
 * Copyright (c) 2015-2016, smeooncun 失色 (semooncun@foxmail.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lubiaoqing.jsite;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.lubiaoqing.jsite.controller.admin._IndexController;
import com.lubiaoqing.jsite.controller.admin._UserController;
import com.lubiaoqing.jsite.controller.admin._WebsiteController;
import com.lubiaoqing.jsite.controller.front.IndexController;
import com.lubiaoqing.jsite.handler.JHandler;
import com.lubiaoqing.jsite.model.User;
import com.lubiaoqing.jsite.util.AdminMenu;

public class JSite extends JFinalConfig {

	public void configConstant(Constants me) {
		loadPropertyFile("jsite.properties");
		PropKit.use("jsite.properties");
		me.setDevMode(getPropertyToBoolean("dev_mode", false));
		me.setViewType(ViewType.FREE_MARKER);
	}

	public void configHandler(Handlers me) {
		me.add(new JHandler());
	}

	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
	}

	public void configPlugin(Plugins me) {
		DruidPlugin dp = new DruidPlugin(getProperty("jdbcUrl"),
				getProperty("user"), getProperty("password").trim());
		me.add(dp);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		me.add(arp);
		arp.addMapping("user", User.class);
	}

	public void configRoute(Routes me) {
		// front
		me.add("/", IndexController.class);

		// admin
		me.add("/admin", _IndexController.class);
		me.add("/admin/user", _UserController.class);
		me.add("/admin/website", _WebsiteController.class);
	}

	public void afterJFinalStart() {
		AdminMenu.init();
	}
}
